package com.binguo.api.controller;


import com.alibaba.fastjson.JSONObject;
import com.binguo.api.form.ShangMiForm;
import com.binguo.api.form.YiZhiFuTXTForm;
import com.binguo.api.form.YizhifuQueryRechargeForm;
import com.binguo.api.form.YizhifuRechargeForm;
import com.binguo.api.result.Result;
import com.binguo.api.result.ResultGenerator;
import com.binguo.api.schedule.DataLoadService;
import com.binguo.api.service.YizhifuService;
import com.binguo.model.api.vo.YizhifuBanlanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * @author yuritian
 * @Title: YizhifuController
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-07-0913:29
 */
@Api(tags = "翼支付接口")
@RestController
@RequestMapping
public class YizhifuController {

    private final Integer SHOPID = 74;

    @Autowired
    YizhifuService yizhifuService;

    @PostMapping(value = "/yxdx/YKT_YHXXYZ.action", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户查询", notes = "通过手机号给手机号进行用户查询")
    public String getcustomer(@RequestParam(value="data", required=true) String data) {
        JSONObject result = new JSONObject();
        JSONObject urlData = JSONObject.parseObject(data);
        StringBuilder content = new StringBuilder();
        String versionId = urlData.getString("VersionId");
        String clientId = urlData.getString("ClientID");
        String merchantid = urlData.getString("MerchantID");
        String phoneNumber = urlData.getString("PhoneNumber");
        String iccid = urlData.getString("ICCID");
        String mac = urlData.getString("MAC");
        String name = urlData.getString("Name");

        content.append(versionId).append(clientId)
                .append(merchantid).append(phoneNumber)
                .append(iccid).append(DataLoadService.shopYiZhiFuMap.get(SHOPID).getShopKey());

        if (yizhifuService.checkSign(content.toString(),mac)){
            return yizhifuService.getCustomer(phoneNumber,name).toJSONString();
        }else {
            result.put("ResultCode","0002");
            result.put("ResultMsg","报文MAC验证失败");
            return result.toJSONString();
        }
    }

    @PostMapping(value = "/yxdx/YKT_YHCZ.action", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户充值", notes = "通过手机号给手机号绑定的用户进行充值")
    public String recharge(@RequestParam(value="data", required=true) String data) {

        JSONObject result = new JSONObject();
        JSONObject urlData = JSONObject.parseObject(data);

        String versionId = urlData.getString("VersionId");
        String clientId = urlData.getString("ClientID");
        String merchantid = urlData.getString("MerchantID");
        String phoneNumber = urlData.getString("PhoneNumber");
        String iccid = urlData.getString("ICCID");
        String orderSeq = urlData.getString("OrderSeq");
        String orderId = urlData.getString("OrderId");
        String tranSeq = urlData.getString("TranSeq");
        String amount = urlData.getString("Amount");
        String orderDate = urlData.getString("OrderDate");
        String mac = urlData.getString("MAC");
        String name = urlData.getString("Name");


        StringBuilder content = new StringBuilder();
        content.append(versionId).append(clientId).append(merchantid)
                .append(phoneNumber).append(iccid).append(orderId)
                .append(orderSeq).append(tranSeq).append(amount)
                .append(orderDate).append(DataLoadService.shopYiZhiFuMap.get(SHOPID).getShopKey());

        YizhifuRechargeForm form = new YizhifuRechargeForm();
        form.setAmount(amount);
        form.setOrderDate(orderDate);
        form.setOrderNum(orderId);
        form.setOrderSeq(orderSeq);
        form.setPhone(phoneNumber);
        form.setName(name);
        if (yizhifuService.checkSign(content.toString(),mac)){
            return yizhifuService.rechargeCustomer(form).toJSONString();
        }else {
            result.put("ResultCode","0002");
            result.put("ResultMsg","报文MAC验证失败");
            return result.toJSONString();
        }
    }

    @PostMapping(value = "/yxdx/YKT_YHYECX.action", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户查询余额", notes = "通过手机号查询余额")
    public String queryBalance(@RequestParam(value="data", required=true) String data) {
        JSONObject result = new JSONObject();
        JSONObject urlData = JSONObject.parseObject(data);

        String versionId = urlData.getString("VersionId");
        String clientId = urlData.getString("ClientID");
        String merchantid = urlData.getString("MerchantID");
        String phoneNumber = urlData.getString("PhoneNumber");
        String iccid = urlData.getString("ICCID");
        String mac = urlData.getString("MAC");
        String name = urlData.getString("Name");


        StringBuilder content = new StringBuilder();
        YizhifuBanlanceVO vo = new YizhifuBanlanceVO();
        content.append(versionId).append(clientId).append(merchantid).append(phoneNumber).append(iccid).append(DataLoadService.shopYiZhiFuMap.get(SHOPID).getShopKey());

        if (yizhifuService.checkSign(content.toString(),mac)){
            return yizhifuService.queryBalance(phoneNumber,name).toJSONString();
        }else {
            result.put("ResultCode","0002");
            result.put("ResultMsg","报文MAC验证失败");
            result.put("Amount","0");
            return result.toJSONString();
        }
    }

    @PostMapping(value = "/yxdx/YKT_QUERY_CZ.action", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户查询充值结果", notes = "通过订单号流水号查询充值结果")
    public String queryRecharge(@RequestParam(value="data", required=true) String data) {
        JSONObject result = new JSONObject();
        JSONObject urlData = JSONObject.parseObject(data);
        String merchantid = urlData.getString("MerchantID");
        String phoneNumber = urlData.getString("PhoneNumber");
        String iccid = urlData.getString("ICCID");
        String orderSeq = urlData.getString("OrderSeq");
        String orderId = urlData.getString("OrderId");
        String amount = urlData.getString("Amount");
        String mac = urlData.getString("MAC");
        String name = urlData.getString("Name");


        StringBuilder content = new StringBuilder();
        content.append(merchantid).append(phoneNumber).append(iccid)
                .append(orderId).append(orderSeq).append(amount)
                .append(DataLoadService.shopYiZhiFuMap.get(SHOPID).getShopKey());
        YizhifuQueryRechargeForm form = new YizhifuQueryRechargeForm();
        form.setAmount(amount);
        form.setOrderNum(orderId);
        form.setOrderSeq(orderSeq);
        form.setPhone(phoneNumber);
        form.setName(name);
        if (yizhifuService.checkSign(content.toString(),mac)){
            return yizhifuService.queryRecharge(form).toJSONString();
        }else {
            result.put("ResultCode","0002");
            result.put("ResultMsg","报文MAC验证失败");
            return result.toJSONString();
        }
    }

    @PostMapping(value = "/api/yizhifu/txt", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户查询余额", notes = "通过手机号查询余额")
    public Result test(@RequestBody @Valid YiZhiFuTXTForm form){
        try {
            yizhifuService.createDayTXT(form.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult("success");

    }
}
